﻿using Application.Services.Interface;
using Application.Services.Interface.Organization;
using Autofac;
using Domain.Interface.Organization;
using Framework.Domain.Interface.DomainObject.UnitOfWork;
using Framework.Domain.Interface.Factorys;
using Repository.Interface.Organization;
using System;
using System.Collections.Generic;
using System.Text;

namespace Application.Services
{
    public class BaseAppServices : IBaseAppService
    {
        public BaseAppServices()
        {
            this.UnitOfWork = AppUnitOfWorkFactory.GetAppUnitOfWorkRepository();
        }

        #region Build Domain对象
        protected IUserDomain BuildUserDomain(string name)
        {
            return ExampleFactory.CreateAppService<IUserDomain>(new NamedParameter("name", name));
        }


        protected IDepartmentDomain BuildDepartmentDomain(string name)
        {
            return ExampleFactory.CreateAppService<IDepartmentDomain>(new NamedParameter("name", name));
        }
        #endregion

        /// <summary>
        /// 工作单元
        /// </summary>
        private IAppUnitOfWork UnitOfWork;


        /// <summary>
        /// 保存操作
        /// </summary>
        protected int SaveChanges()
        {
            return this.UnitOfWork.SaveChanges();
        }



        private IUserRepository userRepository;
        protected IUserRepository UserRepository
        {
            get
            {
                if (this.userRepository == null)
                {
                    this.userRepository = ExampleFactory.CreateAppService<IUserRepository>();
                }
                return this.userRepository;
            }
        }

        private IDepartmentRepository departmentRepository;
        protected IDepartmentRepository DepartmentRepository
        {
            get
            {
                if (this.departmentRepository == null)
                {
                    this.departmentRepository = ExampleFactory.CreateAppService<IDepartmentRepository>();
                }
                return this.departmentRepository;
            }
        }


        private IUserAppService userAppService;
        protected IUserAppService UserAppService
        {
            get
            {
                if (this.userAppService == null)
                {
                    this.userAppService = ExampleFactory.CreateAppService<IUserAppService>();
                }

                return this.userAppService;
            }
        }



    }
}
